﻿using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;

namespace ConfExpert.Forms.Rapor.Aksesuar
{
    public partial class xrAksesuarSiparisFoyEN : DevExpress.XtraReports.UI.XtraReport
    {
        DataSet ds;

        public xrAksesuarSiparisFoyEN()
        {
            InitializeComponent();
        }

        bool IsTedarikciShown = false;

        public void iGetByPOId(string AksPOIds)
        {
            string spName = "GetAksesuarSiparisFoyu";
            string relationColumn = "AksPOId";

            ds = Session.DataManager.ExecuteDataSet(spName, AksPOIds, false);

            bool IsWaterMark = ds.Tables[0].Rows[0]["stTalepId"].ToInt() < 2;
            Watermark.Text = IsWaterMark ? "Önizleme" : "";

            barcode.ShowText = false;
            barcode.Visible = relationColumn == "AksPOId";
            IsTedarikciShown = spName == "GetAksesuarSiparisFoyuBySipId";
            //trRef.Visible = false;

            DataRelation FK_AksPO_AksPOLines = new DataRelation("FK_AksPO_AksPOLines",
              ds.Tables["Table"].Columns[relationColumn],
              ds.Tables["Table1"].Columns[relationColumn]);
            ds.Relations.Add(FK_AksPO_AksPOLines);

            BindDstoReport();
        }

        public void iGetByPOSipId(string AksPOIds)
        {
            string spName = "GetAksesuarSiparisFoyuBySipId";
            string relationColumn = "AksPOSipId";

            ds = Session.DataManager.ExecuteDataSet(spName, AksPOIds, false);

            bool IsWaterMark = ds.Tables[0].Rows[0]["stTalepId"].ToInt() < 2;
            Watermark.Text = IsWaterMark ? "Önizleme" : "";

            barcode.ShowText = false;
            barcode.Visible = relationColumn == "AksPOId";
            IsTedarikciShown = spName == "GetAksesuarSiparisFoyuBySipId";
            //trRef.Visible = false;

            DataRelation FK_AksPO_AksPOLines = new DataRelation("FK_AksPO_AksPOLines",
              ds.Tables["Table"].Columns[relationColumn],
              ds.Tables["Table1"].Columns[relationColumn]);
            ds.Relations.Add(FK_AksPO_AksPOLines);

            BindDstoReport();
        }


        //public xrAksesuarSiparisFoyEN(string AksPOIds, string spName, string relationColumn)
        //    : this()
        //{
        //    ds = Session.DataManager.ExecuteDataSet(spName, AksPOIds);

        //    bool IsWaterMark = ds.Tables[0].Rows[0]["stTalepId"].ToInt() < 2;
        //    Watermark.Text = IsWaterMark ? "Önizleme" : "";

        //    barcode.ShowText = false;
        //    barcode.Visible = relationColumn == "AksPOId";
        //    IsTedarikciShown = spName == "GetAksesuarSiparisFoyuBySipId";
        //    //trRef.Visible = false;

        //    DataRelation FK_AksPO_AksPOLines = new DataRelation("FK_AksPO_AksPOLines",
        //      ds.Tables["Table"].Columns[relationColumn],
        //      ds.Tables["Table1"].Columns[relationColumn]);
        //    ds.Relations.Add(FK_AksPO_AksPOLines);

        //    BindDstoReport();
        //}

        void BindDstoReport()
        {
            this.DataMember = "Table";
            this.DataSource = ds;

            tcDate.Text = DateTime.Now.ToShortDateString();
            tc1.DataBindings.Add("Text", null, "Table.Baslik");
            tcPO.DataBindings.Add("Text", null, "Table.SipNo");
            tcCustomer.DataBindings.Add("Text", null, "Table.Sezon");
            tcSeason.DataBindings.Add("Text", null, "Table.OrderRef");
            tcOrderRef.DataBindings.Add("Text", null, "Table.SiparisiVeren");
            tcRef.DataBindings.Add("Text", null, "Table.POId");
            barcode.DataBindings.Add("Text", null, "Table.AksPOId");

            //////if (IsTedarikciShown)
            //////{
            //////    tcPurchaser.DataBindings.Add("Text", null, "Table.Tedarikci");
            //////    tc12.Dispose();
            //////    tcQuantity.Dispose();
            //////    trRef.Dispose();
            //////}
            //////else
            //////{
            //////    tcSupplier.DataBindings.Add("Text", null, "Table.Musteri");
            //////    tcQuantity.DataBindings.Add("Text", null, "Table.FK_AksPO_AksPOLines.DevamiVarMi");

            //////}

            tcPurchaser.DataBindings.Add("Text", null, "Table.SatinAlan");

            this.DetailReport.DataMember = "Table.FK_AksPO_AksPOLines";
            this.DetailReport.DataSource = ds;

            tcCustomerCode.DataBindings.Add("Text", null, "Table.FK_AksPO_AksPOLines.Malzeme");
            tcDescription.DataBindings.Add("Text", null, "Table.FK_AksPO_AksPOLines.StokKod");
            tcOzellik.DataBindings.Add("Text", null, "Table.FK_AksPO_AksPOLines.Ozellik");

            tcTerminTarihi.DataBindings.Add("Text", null, "Table.FK_AksPO_AksPOLines.Termin");
            tcWidth.DataBindings.Add("Text", null, "Table.FK_AksPO_AksPOLines.Notes");
            tcNo.DataBindings.Add("Text", null, "Table.FK_AksPO_AksPOLines.Nr");
            //////tcSample.DataBindings.Add("Text", null, "Table.FK_AksPO_AksPOLines.Numune");
            tcQuantity.DataBindings.Add("Text", null, "Table.FK_AksPO_AksPOLines.Adet", "{0:#,#}");

            tcDescription.BeforePrint += cell_BeforePrint;
        }

        void cell_BeforePrint(object sender, PrintEventArgs e)
        {
            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            gr.PageUnit = GraphicsUnit.Millimeter;
            int factor = 10;

            XRLabel label = (XRLabel)sender;

            float newSize = 9.75F;
            label.Font = new Font("Calibri", newSize);

            float stringWidth = gr.MeasureString(label.Text, label.Font).Width;
            float labelWidth = label.WidthF;


            if (labelWidth < stringWidth * factor)
                newSize = label.Font.Size * label.Width / stringWidth / factor;

            label.Font = new Font("Calibri", newSize);
            gr.Dispose();
        }

    }
}
